package com.onedec.finance.controller;

import com.onedec.finance.entity.OnedecAccount;
import com.onedec.finance.servier.OnedecAccountService;
import com.onedec.utils.ResponseEntity;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Log4j2
public class AccountController {
    @Autowired
    private OnedecAccountService accountService;
    /**
     * 通过id查询
     */
    @GetMapping("selectById")
    public ResponseEntity selectById(int accountId){
        OnedecAccount account = accountService.selectById(accountId);
        log.info("通过id查询到的信息："+account);
        return new ResponseEntity(account);
}
    /**
     * 查询全部
     */
    @GetMapping("selectAllAccount")
    public String selectAllAccount(){
        return "成功";
    }
    /**
     * 求收入（将所有的收入金额都取出来然后用循环相加得到最后总金额）
     */
    @GetMapping("selectIncome")
    public ResponseEntity selectnum(){
        double num=0;
        double sum=0;
        List<OnedecAccount> selectnum = accountService.selectAllFinance();
        System.out.println("selectnum"+selectnum);
        if (selectnum.size()!=0){
            for (int i=0;i<selectnum.size();i++){
                num+=selectnum.get(i).getAccountTotal();
                sum+=selectnum.get(i).getAccountBak1();
            }
            log.info("总价:{}"+num);
            double income[]={num,sum};
            System.out.println(income[0]);
            System.out.println(income[1]);
            return new ResponseEntity(income);
        }
        return new ResponseEntity("还没有盈利哦，继续努力吧！","400");
    }
    /**
     * 根据月份查询收入
     */
    @GetMapping("selectMonth")
    public ResponseEntity selectMonth(){
        List<Map<String, Object>> maps = accountService.selectMonth();
        System.out.println("打印每月的金额123"+maps);
        return new ResponseEntity(maps);
    }
    /**
     * 根据月份查询支出
     */
    @GetMapping("selectMonths")
    public ResponseEntity selectMonths(){
        List<Map<String, Object>> maps=accountService.selectMonths();
        System.out.println("打印每月的金额"+maps);
        return new ResponseEntity(maps);
    }
}
